
if ( typeof Skt.Users === 'undefined' )
	Skt.Users = {};


Skt.Users.setup = function(sketchpad) {
	sketchpad.on('usersManagerUsersUpdate', Skt.Users.handleUsersUpdate, Skt.Users);
	sketchpad.on('usersManagerStateUpdate', Skt.Users.handleUsersUpdate, Skt.Users);
	sketchpad.on('teacherTrackerUpdate', Skt.Users.handleTeacherTrackerUpdate, Skt.Users);
	sketchpad.on('editorCurrentCanvasChanged', Skt.Users.handleEditorCurrentCanvasChanged, Skt.Users);
	sketchpad.on('roomModeratorUpdate', Skt.Users.handleRoomModeratorUpdate, Skt.Users);
	sketchpad.on('usersManagerRequestsUpdate', Skt.Users.handleUsersManagerRequestsUpdate, Skt.Users);
	
	sketchpad.getUsersManager().refreshUsers();
	Skt.Users.updateUpperToolbarOptions(sketchpad);
};

Skt.Users.handleRemoveEditor = function(userId) {
	var room = Skt.sketchpad.getCurrentRoom();
	if ( room != null )
		room.getRoomModerator().removeEditor(Number(userId));
	
	setTimeout(function(){
		Skt.Users.updateUpperToolbarOptions(Skt.sketchpad);
	}, 100);
};

Skt.Users.handleGrantEditionPermission = function(userId) {
	var room = Skt.sketchpad.getCurrentRoom();
	if ( room != null )
		room.getRoomModerator().addEditor(Number(userId));
	
	setTimeout(function(){
		Skt.Users.updateUpperToolbarOptions(Skt.sketchpad);
	}, 100);
};

Skt.Users.handleUsersManagerRequestsUpdate = function(params) {
	
	console.log('Skt.Users.handleUsersManagerRequestsUpdate');
	
	var user = Skt.sketchpad.getUser();
	var usersManager = Skt.sketchpad.getUsersManager();
	var requester = usersManager.getUser(params.userId);
		
	if ( user.type == Skt.UserType.TEACHER ) {
		Skt.Util.addNotification(requester.name + ' is requesting permission to edit the current slide',
				'Skt.Users.handleGrantEditionPermission('+ params.userId +')',
				';');
	}
};

Skt.Users.handleRequestEditPermissionClick = function() {
	var user = Skt.sketchpad.getUser();
	var usersManager = Skt.sketchpad.getUsersManager();
	var room = Skt.sketchpad.getCurrentRoom();
	
	console.log('usersManager.issueEditionRequest(user.id, room.getId());');
	usersManager.issueEditionRequest(user.id, room.getId());
	
	Skt.Util.addNotification('A petition to edit the current slide has been requested to the teacher');
};

Skt.Users.handleRoomModeratorUpdate = function(params) {
	setTimeout(function(){
		Skt.Users.updateEditingUsers();
		Skt.Users.updateUpperToolbarOptions(Skt.sketchpad);
	}, 100);
};

Skt.Users.handleEditorCurrentCanvasChanged = function(params) {
	var teacherTracker = Skt.sketchpad.getTeacherTracker();
	var newCanvas = params.newCanvas;
	var user = Skt.sketchpad.getUser();

	if ( user.type == Skt.UserType.TEACHER ) {
		if ( newCanvas != null )
			teacherTracker.setTeacherRoomId(newCanvas.getRoom().getId());
		else
			teacherTracker.setTeacherRoomId(null);
	} else {
		if ( teacherTracker.isFollowingEnabled() ) {
			var editor = Skt.sketchpad.getEditor();
			var teacherRoomId = teacherTracker.getTeacherRoomId();
			
			if ( newCanvas == null && teacherRoomId != null ) {
				editor.setCurrentCanvas(teacherRoomId);
			} else {
				var room = newCanvas.getRoom();
				var type = room.getType();
				if ( type == Skt.SlideType.SCREEN && room.getId() != teacherRoomId ) {
					editor.setCurrentCanvas(teacherRoomId);
				}
			}
		}
	}
	
	setTimeout(function(){
		Skt.Users.updateUpperToolbarOptions(Skt.sketchpad);
		Skt.Users.updateEditingUsers();
	}, 100);
};

Skt.Users.handleTeacherTrackerUpdate = function(params) {
	var followingEnabled = params.followingEnabled;
	var teacherRoomId = params.teacherRoomId;

	setTimeout(function(){
		if ( followingEnabled
				&& teacherRoomId != null
				&& Skt.sketchpad.getUser().type == Skt.UserType.STUDENT ) {
			var room = Skt.sketchpad.getCurrentRoom();
			
			if ( room == null
					|| (room.getType() == Skt.SlideType.SCREEN && room.getId() != teacherRoomId) )
				Skt.sketchpad.getEditor().setCurrentCanvas(teacherRoomId);
		}
		
		Skt.Users.updateUpperToolbarOptions(Skt.sketchpad);	
	}, 100);
};

Skt.Users.handleEnableFollowingButtonClick = function() {
	Skt.sketchpad.getTeacherTracker().toggleFollowingEnabled();
};

Skt.Users.handleUsersUpdate = function(params) {
	setTimeout(function(){
		Skt.Users.updateUsersList(Skt.sketchpad.getUsersManager());
	}, 100);
};

Skt.Users.updateEditingUsers = function() {
	$("#upper-toolbar-users-concurrent-list").html("");
	
	var room = Skt.sketchpad.getCurrentRoom();
	var usersManager = Skt.sketchpad.getUsersManager();
	
	if ( room != null ) {
		var html = "";
		var users = room.getRoomModerator().getCurrentlyEditing();
		for ( var i = 0; i < users.length; ++i ) {
			var user = usersManager.getUser(users[i]);
			if ( user.name != null && user.name != "" ) {
				html += '<span class="editing-user">' + user.name;
				if ( room.getType() == Skt.SlideType.SCREEN 
						&& room.getOwnerId() == Skt.sketchpad.getUser().id
						&& Skt.sketchpad.getUser().id != user.id ) {
					html += '<span class="icon-remove" onclick="Skt.Users.handleRemoveEditor(' + user.id + ')"></span>';
				}
					
				html += '</span>';
			}
		}
		$("#upper-toolbar-users-concurrent-list").html(html);
	}
};

Skt.Users.updateUpperToolbarOptions = function(sketchpad) {
	var user = sketchpad.getUser();
	
	$("#upper-toolbar-options").html("");
	
	if ( user.type == Skt.UserType.TEACHER ) {
		var teacherTracker = sketchpad.getTeacherTracker();
		
		var html = '<span id="enable-following-button"'; 
		html += ' onclick="Skt.Users.handleEnableFollowingButtonClick();"'; 
		html += ' class="upper-toolbar-button btn btn-mini';
		if ( teacherTracker.isFollowingEnabled() )
			html += ' active';
		html += '">';
		if ( teacherTracker.isFollowingEnabled() )
			html += 'Disable Following';
		else
			html += 'Enable Following';
		html += '</span>';
		$("#upper-toolbar-options").html(html);
		
	} else {
		var room = sketchpad.getCurrentRoom();
		console.log(room);
		if ( room != null 
				&& room.getType() == Skt.SlideType.SCREEN 
				&& !room.getRoomModerator().canEdit(user) ) {
			var html = '<span id="request-edit-permission"'; 
			html += ' onclick="Skt.Users.handleRequestEditPermissionClick();"'; 
			html += ' class="upper-toolbar-button btn btn-mini';
			html += '">';
			html += 'Request Edition';
			html += '</span>';
			$("#upper-toolbar-options").html(html);
		}
	}
	
};

Skt.Users.updateUsersList = function(usersManager) {
	var html = "";
	var users = usersManager.getUsers();
	var user = null;
	
	for ( var id in users ) {
		user = users[id];
		if ( user.name != null ) {
			html += '<li data-id="' + id + '" class="student-li">';
			html += '<a>';
			html += user.name + ' ' + user.lastname;
			html += '<span id="user-status-' + id + '" class="user-status';
			if ( user.instances > 0 )
				html += ' user-status-online';
			html += '">&#149;</span>';
			html += '</a>';
			html += '</li>';
		}
	}
	
	$("#users-sidebar-body-list-ul").html(html);
	Skt.Groups.setupDragStudents(Skt.sketchpad);
};

